package com.cll.wifistatedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 判断当前的网络类型
 */
public class MainActivity extends AppCompatActivity {

    private ImageView tvText;
    Handler handler = new Handler(Looper.getMainLooper());
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (connectivityManager != null) {
                // 获取活动网络信息
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

                getNetType(activeNetworkInfo);
            }

            handler.postDelayed(runnable, 5000);

        }
    };
    private ConnectivityManager connectivityManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        tvText = findViewById(R.id.tv_menu);
        handler.post(runnable);
    }

    private void getNetType(NetworkInfo info) {
        if (info == null) {
            tvText.setImageResource(R.drawable.ic_no);
            return;
        }
        switch (info.getType()) {
            case ConnectivityManager.TYPE_WIFI://wifi
            case ConnectivityManager.TYPE_WIMAX:
            case ConnectivityManager.TYPE_ETHERNET:
                tvText.setImageResource(R.drawable.wifi);
                break;
            case ConnectivityManager.TYPE_MOBILE:
                switch (info.getSubtype()) {
                    case TelephonyManager.NETWORK_TYPE_NR:
                        //这里对应的值为20 只有依赖为android 10.0才有此属性
                        tvText.setImageResource(R.drawable.icon_5g);
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:  // 4G
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                        tvText.setImageResource(R.drawable.icon_4g);

                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        tvText.setImageResource(R.drawable.icon_3g);

                        break;
                    case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                        tvText.setImageResource(R.drawable.icon_2g);

                        break;
                    default:
                        tvText.setImageResource(R.drawable.ic_no);

                        break;
                }
                break;
            default:
                break;
        }
    }
}